From d582327a632077fa6fcc7849caea67fa515b3b48 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=C3=98yvind=20Kol=C3=A5s?= Date: Sat, 21 Nov 2009 16:42:43 +0000 Subject: [PATCH] Added a mutex to memory sanity counters. Also added a BABL_DEBUG_MEM define which is set to 1 in babl-internal.h that can be used to disable these locks which only are nedded when debugging. --- babl/babl-internal.c | 14 +++++++++++++- babl/babl-internal.h | 5 +++++ babl/babl-memory.c | 34 ++++++++++++++++++++++++++++++++++ babl/babl.c | 2 ++ 4 files changed, 54 insertions(+), 1 deletion(-) diff --git a/babl/babl-internal.c b/babl/babl-internal.c index 7026370..f8689ce 100644 --- a/babl/babl-internal.c +++ b/babl/babl-internal.c @@ -102,9 +102,10 @@ babl_process (Babl *babl, babl->class_type == BABL_FISH_SIMPLE) { long ret; - long ticks = babl_ticks (); + /* long ticks = babl_ticks (); */ ret = babl_fish_process (babl, source, destination, n); + /* XX: ticks -= babl_ticks (); ticks *= -1L; @@ -112,6 +113,7 @@ babl_process (Babl *babl, babl->fish.usecs += ticks; babl->fish.processings++; babl->fish.pixels += ret; + */ return ret; } @@ -119,16 +121,26 @@ babl_process (Babl *babl, return -1; } +#if BABL_DEBUG_MEM +BablMutex *babl_debug_mutex; +#endif + void babl_internal_init (void) { babl_set_malloc (malloc); babl_set_free (free); +#if BABL_DEBUG_MEM + babl_debug_mutex = babl_mutex_new (); +#endif } void babl_internal_destroy (void) { +#if BABL_DEBUG_MEM + babl_mutex_destroy (babl_debug_mutex); +#endif } diff --git a/babl/babl-internal.h b/babl/babl-internal.h index bb71c2d..7606cac 100644 --- a/babl/babl-internal.h +++ b/babl/babl-internal.h @@ -240,6 +240,11 @@ babl_fatal (const char *format, ...) extern int babl_hmpf_on_name_lookups; +#define BABL_DEBUG_MEM 1 +#if BABL_DEBUG_MEM +extern BablMutex *babl_debug_mutex; +#endif + const char *babl_class_name (BablClassType klass); void babl_internal_init (void); void babl_internal_destroy (void); diff --git a/babl/babl-memory.c b/babl/babl-memory.c index 3a8f34f..304651c 100644 --- a/babl/babl-memory.c +++ b/babl/babl-memory.c @@ -54,6 +54,8 @@ typedef struct #define BAI(ptr) ((BablAllocInfo *) *((void **) ptr - 1)) #define IS_BAI(ptr) (BAI (ptr)->signature == signature) +#if BABL_DEBUG_MEM + /* runtime statistics: */ static int mallocs = 0; static int frees = 0; @@ -72,6 +74,8 @@ mem_stats (void) return buf; } +#endif + static void functions_sanity (void) { @@ -113,7 +117,11 @@ babl_malloc (size_t size) *((void **) ret - 1) = ret - BABL_ALLOC - offset; BAI (ret)->signature = signature; BAI (ret)->size = size; +#if BABL_DEBUG_MEM + babl_mutex_lock (babl_format_mutex); mallocs++; + babl_mutex_unlock (babl_format_mutex); +#endif return (void *) (ret); } @@ -131,8 +139,12 @@ babl_dup (void *ptr) ret = babl_malloc (BAI (ptr)->size); memcpy (ret, ptr, BAI (ptr)->size); +#if BABL_DEBUG_MEM + babl_mutex_lock (babl_format_mutex); dups++; mallocs--; +#endif + babl_mutex_unlock (babl_format_mutex); return NULL; } @@ -180,7 +192,11 @@ babl_free (void *ptr, { BAI (format->image_template)->signature = NULL; free_f (BAI (format->image_template)); +#if BABL_DEBUG_MEM + babl_mutex_lock (babl_format_mutex); frees++; + babl_mutex_unlock (babl_format_mutex); +#endif } format->image_template = NULL; } @@ -196,7 +212,11 @@ babl_free (void *ptr, functions_sanity (); BAI (ptr)->signature = NULL; free_f (BAI (ptr)); +#if BABL_DEBUG_MEM + babl_mutex_lock (babl_format_mutex); frees++; + babl_mutex_unlock (babl_format_mutex); +#endif } /* reallocate allocation to be in size instead, contents of @@ -237,7 +257,11 @@ babl_realloc (void *ptr, #endif memcpy (ret, ptr, babl_sizeof (ptr)); babl_free (ptr); +#if BABL_DEBUG_MEM + babl_mutex_lock (babl_format_mutex); reallocs++; + babl_mutex_unlock (babl_format_mutex); +#endif return ret; } @@ -259,8 +283,12 @@ babl_calloc (size_t nmemb, memset (ret, 0, nmemb * size); +#if BABL_DEBUG_MEM + babl_mutex_lock (babl_format_mutex); callocs++; mallocs--; + babl_mutex_unlock (babl_format_mutex); +#endif return ret; } @@ -286,8 +314,12 @@ babl_strdup (const char *s) babl_log ("args=(%s): failed", s); strcpy (ret, s); +#if BABL_DEBUG_MEM + babl_mutex_lock (babl_format_mutex); strdups++; mallocs--; + babl_mutex_unlock (babl_format_mutex); +#endif return ret; } @@ -332,6 +364,7 @@ babl_strcat (char *dest, return ret; } +#if BABL_DEBUG_MEM /* performs a sanity check on memory, (checks if number of * allocations and frees on babl memory evens out to zero). */ @@ -348,3 +381,4 @@ babl_memory_sanity (void) } return 0; } +#endif diff --git a/babl/babl.c b/babl/babl.c index 08c4345..6386f60 100644 --- a/babl/babl.c +++ b/babl/babl.c @@ -72,6 +72,8 @@ babl_exit (void) babl_sampling_class_destroy (); babl_type_class_destroy (); babl_internal_destroy (); +#if BABL_DEBUG_MEM babl_memory_sanity (); +#endif } } -- 2.30.2